package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.data.tenant.TenantContextHolder;
import com.pactera.madp.cp.api.dto.track.BehaviorTrackDto;
import com.pactera.madp.cp.api.entity.*;
import com.pactera.madp.cp.common.util.WxApiUtils;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class BehaviorTrackDailyServiceImpl extends BaseBehaviorTrackServer{

    private ICorpService corpService;
    private DailyService dailyService;
    private IWorkDailySendService workDailySendService;

    @Override
    public void handleEvent(BehaviorTrackDto dto, IWorkEmployeeService workEmployeeService, IWorkContactService workContactService, HttpServletResponse response) throws Exception {
        //获取需要发送消息的企业
        List<CorpEntity> corps = corpService.list();
        if(corps.isEmpty()) {
            return;
        }
        for (CorpEntity corp : corps) {
            //设置租户
            TenantContextHolder.setTenantId(corp.getTenantId());
            CorpContextHolder.setCorpId(corp.getCorpId());
            if (dto.getIsSend()) {
                //获取日报数据
                R result = dailyService.reminder(corp.getCorpId(), dto.getDailyType());
                if (StrUtil.isEmpty((String)result.getData())) {
                    return;
                }
                String dailyData = (String)result.getData();
                //日报数据设置
                BehaviorTrackEntity entity = new BehaviorTrackEntity();
                BeanUtil.copyProperties(dto, entity);
                entity.setCorpId(corp.getCorpId());
                entity.setContent(dailyData);
                //事件处理
                baseHandleEvent(entity, workEmployeeService, workContactService, this, response);
                log.info(dailyData + "\n数据日报发送成功");
            }
        }
    }

    @Override
    public void baseHandleEvent(BehaviorTrackEntity entity, IWorkEmployeeService workEmployeeService,
                                IWorkContactService workContactService, BaseBehaviorTrackServer behaviorTrackServer,
                                HttpServletResponse response) throws Exception {

        //数据组装
        Map data = baseBuildData(entity, workEmployeeService, workContactService, behaviorTrackServer);
        //接收人员列表查询
        Integer corpId = CorpContextHolder.getCorpId();
        String toUsers = workDailySendService.getSendEmployee(corpId);

        //获取发送应用id
        String wxCorpId = corpService.getById(corpId).getWxCorpId();
        Integer agentId = corpService.getWxSendMsgAgentIdByWxCorpId(wxCorpId);

        //设置消息体
        BehaviorTrackTextMsg msg = (BehaviorTrackTextMsg)behaviorTrackServer.getBehaviorTrackMsg(toUsers, agentId, "text",data);

        //消息推送
        WxApiUtils.sendMsgToEmployee( msg, entity.getCorpId());
    }

    @Override
    protected Map baseBuildData(BehaviorTrackEntity entity,
                                IWorkEmployeeService workEmployeeService,
                                IWorkContactService workContactService,
                                BaseBehaviorTrackServer behaviorTrackServer){

        String content = buildData(entity);

        log.info(content);

        Map data = new HashMap();
        data.put("content",content);

        return data;
    }

    @Override
    protected String buildData(BehaviorTrackEntity entity) {
        /*List<String> datas = new ArrayList<>();
        String tempContent = entity.getContent();
        DailyRespVo vo = JSONUtil.toBean(tempContent, DailyRespVo.class);
        WorkContactDailyIndexResp contact = vo.getDailyContact();
        WorkContactRoomDailyIndexResp room = vo.getDailyRoom();
        String dailyDate = vo.getDailyDate();

        datas.add(String.valueOf(contact.getTotal()));  //客户总数
        datas.add(String.valueOf(contact.getAddNum())); //新增客户数
        datas.add(String.valueOf(contact.getLoss()));   //流失客户数
        datas.add(String.valueOf(contact.getRealAddNum())); //净增客户数

        datas.add(String.valueOf(room.getRoomTotal())); //客户群数据
        datas.add(String.valueOf(room.getRoomAddNum()));  //客户群新增数据
        datas.add(String.valueOf(room.getContactTotal()));  //群客户总数
        datas.add(String.valueOf(room.getContactAddNum())); //客户群新增客户数
        datas.add(String.valueOf(room.getContactLossNum())); //客户群流失客户数
        datas.add(String.valueOf(room.getContactRealAddNum()));//客户群净增客户数
        datas.add(dailyDate);

        String content = String.format(BehaviorTrackFormatConst.DailyDataFormat, datas.toArray(new String[datas.size()]));

        return content;*/
        return entity.getContent();
    }
}
